/**
 * @author 李桥
 * @date 2018/12/6
 * @Description: 封装对话框类
 */
import * as $ from 'jquery';

/**
 * 私有
 */
//初始化参数
function Init(params) {
    if (params != undefined && params != null) {
        PARAMS = $.extend({},DEFAULTPARAMS, params);
    }
    ContentWidth = config.width - 10;
    ContentHeight = PARAMS.Height - 40;
};

/**
 * 弹窗方法
 * @param params
 * @param caller
 */
function show(config, caller) {

}

export default class Dialog{
    constructor(){

    }
    /**
     * 提示对话框
     * @param params
     */
    static promopt(params){
        Show(params, "Alert");
    }
    /**
     * @param params
     */
    static alert(params){

    }
    static close(){
        $("#DragAbleDialogLayer,#DragAbleDialog").remove();
    }
    /**
     * 弹窗方法
     * @param params
     * @param caller
     */
    show(config,caller){
        var screenWidth = $(window).width();
        var screenHeight = $(window).height();
        var contentWidth = config.width - 10;
        var contentHeight = config.height - 40;
        //在屏幕中显示的位置（正中间）
        var positionLeft = (screenWidth - config.width) / 2 + $(document).scrollLeft();
        var positionTop = (screenHeight - config.height) / 2 + $(document).scrollTop();
        var content = [];
        content.push("<div id=\"dialog-layer\"></div>");
        content.push("<div id=\"dialog\" style=\"width:" + config.width + "px;height:" + config.height + "px;left:" + positionLeft + "px;top:" + positionTop + "px;\">");
        content.push("<div id=\"title\"><span>" + config.title + "</span><span id=\"Close\">&#10005;</span></div>");
        content.push("<div id=\"container\" style=\"width:" + contentWidth + "px;height:" + contentHeight + "px;\">");
        if (caller == "Dialog") {
            content.push("<iframe frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" src=\"" + config.url + "\" ></iframe>");
        }
        else {
            var tipLineHeight = contentHeight - 60;
            content.push("        <table>");
            content.push("            <tr><td id=\"tipLine\" style=\"height:" + tipLineHeight + "px;\">" + config.content + "</td></tr>");
            content.push("            <tr>");
            content.push("                <td id=\"BtnLine\">");
            content.push("                    <input type=\"button\" id=\"dialogConfirmBtn\" value=\"确定\" />");
            if (caller == "Confirm") {
                content.push("                    <input type=\"button\" id=\"dialogCancelBtn\" value=\"取消\" />");
            }
            content.push("                </td>");
            content.push("            </tr>");
            content.push("        </table>");
        }
        content.push("    </div>");
        content.push("</div>");
        $("body").append(content.join("\n"));
        setDialogEvent(caller);
    }
    //设置弹窗事件
    setDialogEvent(config,caller){
        //添加按钮关闭事件
        $("#dialog #close").click(function () { Dialog.close();});
        //添加ESC关闭事件
        $(window).keydown(function(event){
            var event = event||window.event;
            if(event.keyCode===27){
                Dialog.close();
            }
        });
        //添加窗口resize时调整对话框位置
        $(window).resize(function(){
            var screenWidth = $(window).width();
            var screenHeight = $(window).height();
            var positionLeft = parseInt((screenWidth - config.width) / 2+ $(document).scrollLeft());
            var positionTop = parseInt((screenHeight - config.height) / 2+ $(document).scrollTop());
            $("#dialog").css({"top":positionTop+"px","left":positionLeft+"px"});
        });
        $("#dialog #title").DragBySHF($("#dialog"));
        if (caller != "Dialog") {
            $("#dialog #dialogConfirmBtn").click(function () {
                Dialog.Close();
                if ($.isFunction(config.confirmHandler)) {
                    config.confirmHandler();
                }
            })
        }
        if (caller == "Confirm") {
            $("#dialog #dialogCancelBtn").click(function () {
                Dialog.close();
                if ($.isFunction(config.cancelHandler)) {
                    config.cancelHandler();
                }
            })
        }
    }
}